import dao.common.CategoryManager;
import dao.hibernate.CategoryManagerHB;
import models.exceptions.CreateEntityException;
import models.exceptions.DeleteEntityException;
import models.shop.Category;
import org.junit.Test;
import play.test.FunctionalTest;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class CategoryManager_Tests extends FunctionalTest {
    @Test
    public void create_Test() throws CreateEntityException {
        CategoryManager manager = new CategoryManagerHB();
        Category rootCategory = manager.createRoot("Root");
        assertTrue(rootCategory.id > 0);
        assertTrue(rootCategory.id == 1);
        assertTrue(rootCategory.title.equals("Root"));
        assertTrue(rootCategory.leftValue == 1);
        assertTrue(rootCategory.rightValue == 2);
        addSubCategories(rootCategory);

    }

    private void addSubCategories(Category parentCategory) throws CreateEntityException {
        CategoryManager manager = new CategoryManagerHB();
        int rootRightValue = 2;
        int leftValue = 2;
        int rightValue = 3;
        for (int i = 0; i < 10; i++) {
            Category subCategory = manager.create(String.format("Sub Category %1$s", i), parentCategory.id);
            rootRightValue += 2;
            manager.clearCache();
            Category rootCategory = manager.getById(1);
            assertTrue(rootCategory.id == 1);
            assertTrue(rootCategory.leftValue == 1);
            assertTrue(rootCategory.rightValue == rootRightValue);
            manager.clearCache();
            assertTrue(subCategory.leftValue == leftValue);
            assertTrue(subCategory.rightValue == rightValue);
            leftValue += 2;
            rightValue += 2;
        }
        try {
            Category subCategory = new Category();
            subCategory.title = "No parent id";
            manager.save(subCategory);
            assertTrue(false);
        } catch (CreateEntityException ex) {
            assertTrue(ex.getMessage().equals("ParentCategoryId must be set"));
        }
    }

    @Test
    public void listByParent_Test() throws IOException, SQLException {
        CategoryManager manager = new CategoryManagerHB();
        Category root = manager.getById(1);
        assertTrue(root.leftValue == 1);
        assertTrue(root.rightValue == 22);
        List<Category> categories = manager.listByParentId(1);
        assertTrue(categories.size() > 0);
    }

    @Test
    public void deleteItem_Test() throws DeleteEntityException {
        CategoryManager categoryManager = new CategoryManagerHB();
        categoryManager.clearCache();
        int categoryId = 5;
        categoryManager.delete(categoryId);
        long count = categoryManager.count();
        assertTrue(count == 10);
        Category root = categoryManager.getById(1);
        assertTrue(root.leftValue == 1);
        assertTrue(root.rightValue == 20);
    }

    @Test
    public void moveItem_Test(){
        //TODO: Implement test for move when moving feature is needed.
    }

    @Test
    public void editItem_Test(){
        //TODO: Implement test for editing when it's needed
    }
}
